﻿Public Class DialogTextSprachKonverter
    Public Shared SprachKonvIni As DialogTextSprachKonverter = New DialogTextSprachKonverter()
    Dim Sprachen() As Sprache = {}


    ''' <summary>
    ''' Die Funktion gibt False zurueck, wenn sich eine neue ID vergeben werden musste, weil die alte schon vorhanden war. In diesem Fall wird die ID ueberschrieben
    ''' </summary>
    ''' <param name="SprachStr"></param>
    ''' <param name="Text"></param>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function AddEintrag(ByVal SprachStr As String, ByVal Text As String, ByRef ID As Integer) As Boolean
        Dim Spr As Sprache = Nothing
        For Each Sp In Sprachen
            If Sp.SprachStr = SprachStr Then
                Spr = Sp
                Exit For
            End If
        Next
        If Spr Is Nothing Then
            Spr = New Sprache(SprachStr)

            Array.Resize(Sprachen, Sprachen.Length + 1)
            Sprachen(Sprachen.Length - 1) = Spr
        End If

        Return Spr.addText(Text, ID)
    End Function
    Public Shared Sub Reset()
        SprachKonvIni = New DialogTextSprachKonverter
    End Sub

    Function getText(ByVal ID As Integer) As String
        Dim Spr As Sprachen = DataSettings.Sprache
        Return getText(Spr.ToString, ID)
    End Function
    Function getText(ByVal SprachStr As String, ByVal ID As Integer) As String
        For Each Spr In Sprachen
            If Spr.SprachStr.ToLower = SprachStr.ToLower Then
                Return Spr.getText(ID)
            End If
        Next
        Return ""
    End Function

    Function getID(ByVal SprachStr As String, ByVal Text As String) As Integer
        For Each Spr In Sprachen
            If Spr.SprachStr.ToLower = SprachStr.ToLower Then
                Return Spr.getID(Text)
            End If
        Next
        Return 0
    End Function


    Sub WriteKonverter(ByVal strW As IO.StreamWriter)
        If Not Sprachen.Length > 0 Then Exit Sub
        For Each Sprache In Sprachen
            Sprache.WriteSprache(strW)
        Next
    End Sub
    Private Class Sprache
        Public SprachStr As String
        Dim Texte() As String = {}
        Dim IDs() As Integer = {}

        Sub New(ByVal SprachStr As String)
            Me.SprachStr = SprachStr
        End Sub

        Function addText(ByVal Text As String, ByRef ID As Integer) As Boolean
            Dim IDAenderung As Boolean = True
            For Each I In IDs
                If I = ID Then
                    'Die ID gibt's schon, das ist schlecht!
                    ID = getFreieID()
                    IDAenderung = False
                    Exit For
                End If
            Next

            Array.Resize(Texte, Texte.Length + 1)
            Texte(Texte.Length - 1) = Text
            Array.Resize(IDs, IDs.Length + 1)
            IDs(IDs.Length - 1) = ID

            Return IDAenderung
        End Function

        Function getFreieID() As Integer
            Dim i As Integer = 1
            Do While IDVorhanden(i)
                i += 1
            Loop
            Return i
            'Den ueberlauf ruf ich jetzt nicht ab, falls das passiert. Ist entweder irgendwo ein fehler, oder der user hat sich die Finger wund getippt!
        End Function
        Function IDVorhanden(ByVal ID) As Boolean
            For Each i In IDs
                If i = ID Then Return True
            Next
            Return False
        End Function


        Function getText(ByVal ID As Integer) As String
            For i = 0 To Texte.Length - 1
                If IDs(i) = ID Then Return Texte(i)
            Next
            Return ""
        End Function

        Function getID(ByVal Text As String) As Integer
            For i = 0 To Texte.Length - 1
                If Texte(i) = Text Then Return IDs(i)
            Next
            Return 0
        End Function

        Sub WriteSprache(ByVal strW As IO.StreamWriter)
            strW.WriteLine()
            strW.WriteLine("'Language: " + SprachStr)
            For i = 0 To Texte.Length - 1
                strW.WriteLine("%" + SprachStr + "%" + IDs(i).ToString + "%" + Texte(i))
            Next
        End Sub
    End Class

End Class
